package com.zsr.example.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.zsr.example.entity.EduArticle;
import com.zsr.example.service.EduArticleCategoryService;
import com.zsr.example.service.EduArticleService;
import com.zsr.example.service.TOrderService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.util.JwtUtil;
import com.zsr.example.util.constants.BusinessEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhousiren
 * @date 2022/1/6 22:26
 */
@RestController
@RequestMapping("/eduservice/article")
@CrossOrigin
public class ArticleController {
    @Autowired
    private EduArticleService articleService;
    @Autowired
    private EduArticleCategoryService articleCategoryService;
    @Autowired
    private TOrderService orderService;

    @GetMapping("pageArticle/{current}/{limit}")
    public JSONObject pageArticle(@PathVariable long current,
                                      @PathVariable long limit,
                                  @RequestParam(required = false) String categoryId) {

        List<EduArticle> eduArticles = articleService.myQueryPage(current, limit, categoryId);


        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",articleService.count(null));
        map.put("list",eduArticles);
        map.put("categoryList",articleCategoryService.list(null));

        return CommonUtil.successJson(map);
    }

    @GetMapping("getArticle/{id}")
    public JSONObject getArticle(HttpServletRequest request, @PathVariable String id){
        articleService.updatePageViewCount(id);
        //todo 判断是否收藏 点赞
        EduArticle article = articleService.getById(id);
        boolean isBuy = orderService.isBuyCourse(JwtUtil.getMemberIdByJwtToken(request), BusinessEnum.ARTICLE.getType(), id);
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("isBuy",isBuy);
        map.put("article",article);
        return CommonUtil.successJson(map);
    }
}
